//
//  XYJZTViewController.m
//  徙游记-01
//
//  Created by hcios on 2017/7/10.
//  Copyright © 2017年 鹿林. All rights reserved.
//

#import "XYJZTViewController.h"
#import "XYJZTModel.h"
#import "XYJZTTableViewCell.h"

@interface XYJZTViewController ()<UITableViewDelegate,UITableViewDataSource,UIScrollViewDelegate>{
    UITableView *_tableView;
}

@property (nonatomic,strong) NSMutableArray *dataArr;

@property (nonatomic,weak) UITableView *perceivedErrorTableView;

@end

@implementation XYJZTViewController

-(instancetype)init{
    if (self = [super init]) {
        _dataArr = [[NSMutableArray alloc] init];
    }
    return self;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initTable];
    [self netWorking];
    
    self.title = [NSString stringWithFormat:@"%@专题",_cityName];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    NSLog(@"出现");
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"nabBg"] forBarMetrics:UIBarMetricsDefault];
}


-(void)initTable{
    
    _tableView = [[UITableView alloc] initWithFrame:RECT_MAKE(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64) style:UITableViewStylePlain];
    _tableView.rowHeight = 200;
    _tableView.showsVerticalScrollIndicator = NO;
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.backgroundColor = [UIColor whiteColor];
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tableView];
    self.perceivedErrorTableView = _tableView;
    
}



-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataArr.count;
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    XYJZTTableViewCell *cell = [XYJZTTableViewCell perceicedErrorCellFromXib:tableView];
    
    cell.model = _dataArr[indexPath.row];
    
    return cell;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"%ld%ld",(long)[indexPath section],(long)[indexPath row]);
}


-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    NSArray *visibleCells = [self.perceivedErrorTableView visibleCells];
    
    for (XYJZTTableViewCell *cell in visibleCells) {
        [cell cellOnTableView:_tableView didScrollView:self.view];
    }
}


-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [self scrollViewDidScroll:[[UIScrollView alloc] init]];
}

-(void)netWorking{
    NSString *urlString = [NSString stringWithFormat:@"http://chanyouji.com/api/articles.json?destination_id=%@",_selfID];
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    
    [param setValue:@"1" forKey:@"page"];
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager GET:urlString parameters:param progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        for (NSDictionary *dict in responseObject) {
            XYJZTModel *model = [[XYJZTModel alloc] initWithDict:dict];
            [_dataArr addObject:model];
        }
        [_tableView reloadData];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"网络请求失败");
    }];
}



@end
